package com.kj.report.service.impl;

import java.io.IOException;
import java.util.Date;

import com.kj.report.utils.Constants;
import com.kj.report.service.DeviceService;
import com.kj.report.service.WeChatService;
import com.kj.report.utils.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 微信相关处理
 * @author chenkun
 */
@Service
@Transactional
public class WeChatServiceImpl implements WeChatService {

	Logger logger = Logger.getLogger(WeChatServiceImpl.class);
	

	@Autowired
	private RedisCache redisCache;
	
	@Autowired
	private PropertiesUtil propertiesUtil;
	
	@Autowired
	private DeviceService deviceServiceImpl;
	

	@Override
	public String sendReportMsg(String reportId, String openId, String nickName, Date inspectDate,
			String deviceType, Integer deviceReport) {
		logger.info(reportId+"报告解析完成发送图文消息：openId："+openId+"、nickName："+nickName+
				"、inspectDate："+inspectDate+"、deviceType："+deviceType+"、deviceReport："+deviceReport);
		// 获取accessToken
		String token = (String) redisCache.select(Constants.REDISKEY_ACCESS_TOKEN);
		if (token == null) {
			logger.info("access_token为空");
		}
		
		String url = "";
		if (!StringUtil.isStrTrimEmpty(deviceType) && ("KJ201").equals(deviceType)) {
        	url = propertiesUtil.getHtmlSmallDevice() + "/index.html?reportId="+reportId+"&openId="+openId;
		}else {
			if (deviceReport == 5) { // 改善分析版
				url = propertiesUtil.getHtmlBigDevice() + "/index2.html?reportId="+reportId+"&openId="+openId;
			} else if (deviceReport == 100) { // 公益版本
				url = propertiesUtil.getHtmlBigDevice() + "/fund/index.html?reportId="+reportId+"&openId="+openId;	
			} else if (deviceReport == 6) { // 3.0
				url = propertiesUtil.getHtmlBigDevice() + "/index3.html?reportId="+reportId+"&openId="+openId;
			} else { // 普通版本
				url = propertiesUtil.getHtmlBigDevice() + "/index.html?reportId="+reportId+"&openId="+openId;
			}
		}
		
		String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
		requestUrl = requestUrl.replace("ACCESS_TOKEN", token); // 发送客服消息
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("msgtype", "news");
		jsonObject.put("touser", openId);
		JSONObject jsonContent = new JSONObject();
		if (StringUtil.isStrTrimEmpty(nickName)) {
			jsonContent.put("title", "您好！请点击查看报告");
		} else {
			jsonContent.put("title", nickName+",您好！请点击查看报告");
		}
		jsonContent.put("description", "检测时间："+ DateUtil.getDateAP(inspectDate));
		jsonContent.put("url", url);
		jsonContent.put("picurl", "http://www.jiankangzhan.com/img/img_5.jpg");
		
		JSONArray articles = new JSONArray();
		articles.add(jsonContent);
		JSONObject articlesO = new JSONObject();
		articlesO.put("articles", articles);
		jsonObject.put("news", articlesO);
		String jsonStr;
		try {
			jsonStr = HttpUtils.sendPostBuffer(requestUrl, jsonObject.toString());
			logger.info(reportId+"发送报告信息图文消息返回json："+jsonStr);
			return jsonStr;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


	
}
